package com.aopcinemas.visitor.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.apache.commons.lang3.StringUtils;

import com.aopcinemas.cinema.model.Movie;
import com.aopcinemas.cinema.model.Movie.AgeConstraint;
import com.aopcinemas.cinema.model.Movie.Genre;
import com.aopcinemas.cinema.ontology.GetScheduleList;
import com.aopcinemas.common.ui.UIUtils;
import com.aopcinemas.visitor.VisitorAgent;

public class MovieSearchAgentDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private VisitorAgent agent;

    public MovieSearchAgentDialog(JFrame owner, VisitorAgent agent) {
        super(owner, true);
        this.agent = agent;

        JPanel panel = new JPanel();

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(3, 3, 3, 3);
        panel.setLayout(layout);

        final MovieSearchPanel movieSearchPanel = new MovieSearchPanel();

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        layout.setConstraints(movieSearchPanel, constraints);
        panel.add(movieSearchPanel);

        JPanel buttonPanel = new JPanel();
        final JButton removeButton = new JButton("Remove");
        final JButton addButton = new JButton("Add");
        removeButton.setEnabled(false);

        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                MovieSearchAgentDialog.this.agent.setGetScheduleList(null);
                removeButton.setEnabled(false);
                addButton.setEnabled(true);
            }
        });
        buttonPanel.add(removeButton);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                GetScheduleList getScheduleList = new GetScheduleList();

                getScheduleList.setStartTime(UIUtils.mergeDateAndTime(movieSearchPanel.startDate.getDate(), (Date) movieSearchPanel.startTime.getValue()));
                getScheduleList.setEndTime(UIUtils.mergeDateAndTime(movieSearchPanel.endDate.getDate(), (Date) movieSearchPanel.endTime.getValue()));

                Movie movie = new Movie();
                movie.setName(movieSearchPanel.movieName.getText());
                movie.setGenre((Genre) movieSearchPanel.genreList.getSelectedItem());
                movie.setAgeConstraint((AgeConstraint) movieSearchPanel.ageConstraintList.getSelectedItem());
                getScheduleList.setMovie(movie);

                if (StringUtils.isNotBlank(movieSearchPanel.period.getText())) {
                    getScheduleList.setPeriod(Long.parseLong(movieSearchPanel.period.getText()));
                }

                MovieSearchAgentDialog.this.agent.setGetScheduleList(getScheduleList);
                removeButton.setEnabled(true);
                addButton.setEnabled(false);
            }
        });

        buttonPanel.add(addButton);

        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(buttonPanel, constraints);
        panel.add(buttonPanel);

        getContentPane().add(panel);

        setSize(400, 300);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
    }
}
